package com.hand.wfive.ad;

import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.hand.wfive.R;
import com.hand.wfive.base.BaseActivity;
import com.hand.wfive.dialog.VipExpireDialog;
import com.hand.wfive.entity.event.EventBusBean;
import com.hand.wfive.entity.event.EventTagConfig;
import com.hand.wfive.loginAndVip.UserManager;
import com.hand.wfive.loginAndVip.model.UserEvent;
import com.hand.wfive.loginAndVip.ui.VipCenterActivity;
import com.hand.wfive.util.FuncSwitchUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public abstract class AdActivity extends BaseActivity {

    public static String curVideoClass;

    public static long WatchVideoDelay = 2 * 60 * 1000;//视频广告间隔时间
    public static long lastWatchVideo = 0;
    public static long lastShowBanner = 0;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerEventBus();

        ViewGroup bannerView = findViewById(R.id.bannerView);
        if (bannerView != null) showSecondPageAd(bannerView);
    }

    /**
     * 必须vip
     */
    protected void showVip() {
        /*if (BuildConfig.DEBUG){
            adCloseCallBack();
            return;
        }*/
        FuncSwitchUtils.refreshUserStatus();
        curVideoClass = getClass().getName();
        AdManager.getInstance().doVipIntent(mActivity);
    }

    /**
     * 后台开关：开：必须vip  关：功能免费使用一次；
     */
    protected void showVip(String funcType) {
        if (FuncSwitchUtils.canUse(funcType)) {
            adCloseCallBack();
            return;
        }
        curVideoClass = getClass().getName();
        AdManager.getInstance().doVipIntent(mActivity);
    }

    /**
     * 列表里免费查看两条
     */
    protected void showVip(int position) {
        if (FuncSwitchUtils.canUseByPosition(position ,2)) {
            adCloseCallBack();
            return;
        }
        curVideoClass = getClass().getName();
        AdManager.getInstance().doVipIntent(mActivity);
    }

    private void showSecondPageAd(ViewGroup bannerView) {
        if (!AdConfig.isShowAd || AdConfig.adDisable || (AdConfig.isHuawei() && AdConfig.vacation == AdConfig.VACATION_OFF_WORK_NOBANNERAD)) {
            return;
        }
        if (System.currentTimeMillis() - AdActivity.lastShowBanner >= AdActivity.WatchVideoDelay) {
            AdActivity.lastShowBanner = System.currentTimeMillis();
            AdManager.getInstance().setActivity(this).showBannerBottom(bannerView);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void adClose(ADCloseEvent event) {
        if (getClass().getName().equals(curVideoClass)) {
            curVideoClass = null;
            adCloseCallBack();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void doUserEvent(UserEvent event) {
        if (UserManager.getInstance().isVip()
                && getClass().getName().equals(curVideoClass)) {
            openVipCallback();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void doBaseEvent(EventBusBean event) {
        Log.d("testXX", "doEvent :" + event.getTag());
        if (event.getTag() == EventTagConfig.TAG_REFRESH_USER_STATUS) {
            new VipExpireDialog(mContext,
                    R.drawable.btn_dialog_positive,
                    integer -> {
                        VipCenterActivity.show(mContext);
                        return null;
                    }).show();
        }
    }

    protected void adCloseCallBack() {
    }

    protected void openVipCallback() {

    }
}
